package com.activities.group;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONObject;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.Toast;

import com.Utility.MessageManager;
import com.Utility.Utility;
import com.parser.Error_Report;
import com.parser.Parser_Soap;
import com.senditcertified.mail.MainActivity;
import com.senditcertified.mail.R;
import com.senditcertified.mail.R.drawable;
import com.senditcertified.mail.R.layout;
import com.soap.call.Http_Soap_Call;

public class TabHomeGroupActivity extends TabActivity implements OnClickListener {

	private TabHost mTabHost;
	private ArrayList<Activity> HomeChildList = new ArrayList<Activity>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.tab_host);

		mTabHost = getTabHost();
		TabHost.TabSpec spec;
		Resources res = getResources();
		Intent intent;
		intent = new Intent().setClass(this, TabInboxGroupActivity.class);
		spec = mTabHost.newTabSpec(TabInboxGroupActivity.class.getSimpleName())
				.setIndicator(getButton(R.drawable.inbox_tab_selector))
				.setContent(intent);
		mTabHost.addTab(spec);

		/*
		 * mTabWidget.setOnClickListener(new OnClickListener() {
		 * 
		 * @Override public void onClick(View v) { System.out.println("HERE");
		 * onBackPressed(); // TODO Auto-generated method stub
		 * 
		 * } });
		 */

		intent = new Intent().setClass(this, TabOutboxGroupActivity.class);
		spec = mTabHost.newTabSpec(TabOutboxGroupActivity.class.getSimpleName())
				.setIndicator(getButton(R.drawable.outbox_tab_selector))
				.setContent(intent);
		mTabHost.addTab(spec);

		intent = new Intent().setClass(this, TabContactGroupActivity.class);
		spec = mTabHost.newTabSpec(TabContactGroupActivity.class.getSimpleName())
				.setIndicator(getButton(R.drawable.contac_tab_selector))
				.setContent(intent);
		mTabHost.addTab(spec);

		intent = new Intent().setClass(this, TabSendGroupActivity.class);
		spec = mTabHost.newTabSpec(TabSendGroupActivity.class.getSimpleName())
				.setIndicator(getButton(R.drawable.send_tab_selector))
				.setContent(intent);
		mTabHost.addTab(spec);

		intent = new Intent().setClass(this, TabVaultGroupActivity.class);
		spec = mTabHost.newTabSpec(TabVaultGroupActivity.class.getSimpleName())
				.setIndicator(getButton(R.drawable.valut_tab_selector))
				.setContent(intent);
		mTabHost.addTab(spec);

		intent = new Intent().setClass(this, TabSettingsGroupActivity.class);
		spec = mTabHost.newTabSpec(TabSettingsGroupActivity.class.getSimpleName())
				.setIndicator(getButton(R.drawable.setting_tab_selector))
				.setContent(intent);
		mTabHost.addTab(spec);
		if (mTabHost.getTabWidget() != null) {
			for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
				View v = mTabHost.getTabWidget().getChildAt(i);
				v.setTag(i);
				v.setOnClickListener(this);
			}
		}
		mTabHost.setCurrentTab(0);

	}

	public TabHost getHost() {
		return mTabHost;
	}

	public void registerHomeTabClick(Activity activty) {

		HomeChildList.add(activty);
		getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				try {
					for (Activity active : HomeChildList) {

						active.finish();
					}
					HomeChildList.clear();
					mTabHost.setCurrentTab(0);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});

	}

	public class GetDeliveryAcceptance extends AsyncTask<String, Void, Boolean> {
		boolean result = false;
		// String error_msg = "";
		String Result = null;
		Parser_Soap parse = new Parser_Soap();
		Error_Report repor = new Error_Report();

		@Override
		protected Boolean doInBackground(String... params) {
			Http_Soap_Call call = Http_Soap_Call.create_connection("",
					getApplicationContext());
			if (call != null) {
				// Result = parse.GetLoginToken(
				// call.GetLoginToken(Username, params[0], repor), repor);
				Result = parse.GetIsLoginTokenValid(call.GetIsLoginTokenValid(
						Utility.getStringPreference(getApplicationContext(),
								"LoginToken", "1"), params[0], repor), repor);
				if (Result != null) {
					result = true;

				} else {
					// error_msg=Utility.INVALID_USER;
					System.out.println("false");
					result = false;
					repor.setMessage("Internal Error");
				}

			} else {
				repor.setMessage(Utility.NETWORK_ISSUE);
				return false;
			}

			return result;
		}

		@Override
		protected void onPreExecute() {
		}

		protected void onPostExecute(Boolean result) {
			// This method runs in the UI thread

			if (result) {
				// Utility.printLog("Login Responce", "Login successfully");
				// Utility.setStringPreference(getParent(), "LoginToken",
				// Result);
				System.out.println("Parse Result::" + Result);

				try {
					JSONObject js = new JSONObject(Result);
					// JSONObject obj=js.getJSONObject(0);
					Result = js.getString("ReturnValue");

					Utility.setStringPreference(getParent(), "LoginToken",
							Result);
				} catch (Exception e) {
					e.printStackTrace();
					Toast.makeText(getParent(), "Token expired",
							Toast.LENGTH_LONG).show();
					finish();
				}

				if (TextUtils.isEmpty(Result)) {
					Intent intent = new Intent(getApplicationContext(),
							MainActivity.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);
					finish();
				}

			} else {
				// Utility.printLog("Login Responce", "Login Unsuccessfully");
				// Toast.makeText(Password.this, repor.getMessage(),
				// Toast.LENGTH_SHORT).show();
			}

		}
	}

	public ImageButton getButton(int id) {

		ImageButton image = new ImageButton(getApplicationContext());
		image.setImageResource(id);
		image.setBackgroundColor(Color.TRANSPARENT);

		image.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				System.out.println("HHHHH");
				onBackPressed();

			}
		});
		return image;

	}

	@Override
	public void onClick(View v) {
		int pos = 0;
		try {
			pos = Integer.parseInt(v.getTag().toString());
		} catch (NumberFormatException numberFormatException) {
			// DO something
		}
		mTabHost.setCurrentTab(pos);
		Activity currentActivity = getLocalActivityManager().getActivity(
				mTabHost.getCurrentTabTag());
		if (currentActivity != null) {
			TabBaseGroupActivity currentTabGroup = (TabBaseGroupActivity) currentActivity;
			if (currentTabGroup != null) {
				currentTabGroup.clearOnTop();
			}
		}
	}
}
